package com.boot.ys.mybatisplus.utils;

import java.util.ArrayList;
import java.util.List;

import com.boot.ys.mybatisplus.entity.dto.TreeNode;

/**
 * @author: 杨升
 * @date: 2024-08-26 19:04
 * @Version 1.0
 */

public class TreeUtil {

    public static <T extends TreeNode> List<T> build(List<T> treeNodes, Object root) {
        List<T> trees = new ArrayList<>();
        for (T treeNode : treeNodes) {
            if (root.equals(treeNode.getParentId())) {
                trees.add(treeNode);
            }

            for (T it : treeNodes) {
                if (it.getParentId() == treeNode.getId()) {
                    if (treeNode.getChildren() == null) {
                        treeNode.setChildren(new ArrayList<>());
                    }
                    treeNode.add(it);
                }
            }
        }
        return trees;
    }

}
